package com.meituan.hive.udf.generic;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import java.util.List;
import java.util.Map;

/**
 * Created by zhaoxiang on 2017/8/10.
 */
public class GenericUDFTest extends GenericUDF {

    private transient ObjectInspectorConverters.Converter[] converters;
    StringBuilder stringBuilder ;
    private transient MapObjectInspector mapOI;
    private transient ListObjectInspector listOI;

    protected String getFuncName() {
        return getClass().getSimpleName().substring(10).toLowerCase();
    }

    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
        stringBuilder = new StringBuilder();
        stringBuilder.append("ObjectInspector arguments :");
        for(ObjectInspector arg: arguments){
            stringBuilder.append(arg.getClass().getName()+"#"+arg.getCategory()+"#"+arg.getTypeName()+"\n");
            if(arg instanceof MapObjectInspector){
                mapOI = (MapObjectInspector)arg;
                ObjectInspector keyoi = ((MapObjectInspector)arg).getMapKeyObjectInspector();
                ObjectInspector valueoi = ((MapObjectInspector)arg).getMapValueObjectInspector();
                stringBuilder.append("=========="+keyoi.getClass().getName()+"#"+keyoi.getCategory()+"#"+keyoi.getTypeName()+"\n");
                stringBuilder.append("=========="+valueoi.getClass().getName()+"#"+valueoi.getCategory()+"#"+valueoi.getTypeName()+"\n");
            }
            if(arg instanceof ListObjectInspector){
                listOI = (ListObjectInspector)arg;
                ObjectInspector elemOI = listOI.getListElementObjectInspector();
                stringBuilder.append("==========="+elemOI.getClass().getName()+"#"+elemOI.getCategory()+"#"+elemOI.getTypeName()+"\n");
            }
        }

        return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    }

    @Override
    public Object evaluate(GenericUDF.DeferredObject[] arguments) throws HiveException {
        stringBuilder.append("\n");
        stringBuilder.append("Params :");
        for(GenericUDF.DeferredObject deferredObject : arguments){
            stringBuilder.append(deferredObject.getClass().getName()+"#"+deferredObject.get().getClass().getCanonicalName()+"#"+deferredObject.get()+"\n");
            if(deferredObject.get() instanceof List){
                stringBuilder.append(((List)deferredObject.get()).get(0).getClass().getCanonicalName()+"\n");
            }

        }

        return stringBuilder;
    }

    @Override
    public String getDisplayString(String[] children) {
        assert (children.length == 1);
        return getStandardDisplayString("array_add", children);
    }
}
